
import threading, logging, logging.config
from export.localFile import *

class dataExport(threading.Thread):
      def __init__(self, qOut, idEquipC):

          self.queueOut = qOut
          self.logger = logging.getLogger('lector.dataexport')

          (id, self.idEquip) = idEquipC
          self.localFile = localFile(id, self.idEquip)

          threading.Thread.__init__(self)

      def ferMitja(self, mostraLectures):
          mitja = {}
          for variable, valors in mostraLectures.iteritems():
              mitja[variable] = [float(0) for v in valors[0]]
              for v in valors:
                  mitja[variable] = map(lambda x,y:x+y, mitja[variable],[float(e) for e in v])

              mitja[variable] = [float(v)/len(valors) for v in mitja[variable]]

          return mitja

      def run(self):
          while True:
                mitjaDades = self.ferMitja(self.queueOut.get(block=True))
                self.logger.info("Guardant lectura ...")

                # PENDENT: Aqui hem de possar el client del WS (REST)

                self.localFile.mitjaDades = mitjaDades
                self.localFile.save() 

                self.queueOut.task_done() 
